import Mock from "mockjs"
//朱锐
let Zhurui = []
for (let i = 0; i < 100; i++) {
    Zhurui.push({
        id: Mock.mock('@id()'),
        name: Mock.mock('@cname'),
        card: Mock.mock('@natural(6000000, 8000000)'),
        identity: Mock.mock('@natural(421181199911130043, 621181199911130043)'),
        phone: 1353354334,

        office: Mock.mock('@natural(0, 2)'), //挂号状态
        type: Mock.mock('@natural(0, 4)'),  //挂号科室
        idd: Mock.mock('@natural(0, 2)'), //挂号类型
        cost: 14, //挂号费用
        // state: Mock.mock('@natural(0, 2)'), //挂号状态
        time: Mock.mock('@now'),  //就诊日期
        time1: Mock.mock('@time()'),//候诊时间
        time2: Mock.mock('@now')//预约时间
    })
};

//提取url
function queryParse(url) {
    const search = url.split('?')[1] //keyword=%E5%BC%A0%E5%86%9B
    if (!search) {
        return {}
    }
    //{keyword:'张三'}
    return JSON.parse('{"' + decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') + '"}')
}
export default {
    getzhuRui: config => {
        //搜索
        // console.log('config', queryParse(config.url));
        let { keyword, page, pageSize } = queryParse(config.url)
        let res = Zhurui.filter(item => item.name.indexOf(keyword) != -1)
        //排序分页
        let reslLimit = res.filter((item, index) => {
            // console.log( page  * pageSize);
            return (page - 1) * pageSize <= index && index < page * pageSize

        })
        // console.log(reslLimit);
        // Zhurui=reslLimit
        // console.log(reslLimit)
        return {
            code: 200,
            msg: '请求成功',
            data: reslLimit,
            total: Zhurui.length
        }
    },
    DeleteDate: config => {
        // console.log(config);
        let { id } = JSON.parse(config.body)
        let res = Zhurui.filter(item => item.id != id)
        Zhurui = res
        return {
            code: 200,
            msg: '删除成功',
            data: null
        }


    },
    addDate: config => {
        const { name, card, identity, phone, region, type, resource } = JSON.parse(config.body)
        console.log(name);
        // let region1=region.length-1
        console.log(resource);
        
        Zhurui.unshift({
            id: Mock.mock('@id()'),
            name: name,
            card: card,
            identity: identity,
            phone: phone,
            type: region,
            idd: type,
            office: resource,  //挂号状态
            cost: 14, //挂号费用
            time: Mock.mock('@now'),  //就诊日期
            time1: Mock.mock('@time()'),//候诊时间
            time2: Mock.mock('@now')//预约时间

        })
        return {
            code: 200,
            msg: '添加成功',
            data: null
        }
    },
    BianjiDate:config=>{
        const { id,name, card, identity, phone, region, type, resource } = JSON.parse(config.body)
        Zhurui.forEach(item=>{
            if(item.id==id){
                item.name= name,
                item.card= card,
                item.identity= identity,
                item.phone= phone,
                item.type= region,
                item.idd= type,
                item.office= resource,  //挂号状态
                item.cost= 14, //挂号费用
                item.time= Mock.mock('@now'),  //就诊日期
                item.time1= Mock.mock('@time()'),//候诊时间
                item.time2= Mock.mock('@now')//预约时间
    

            }
            console.log(id);
        })
            
           
      


        return {
            code: 200,
            msg: '删除成功',
            data: null
        }

    }
    

}
